/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.quangtin3.sb;

import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.quangtin3.entity.User;

/**
 *
 * @author Quang
 */
@Stateless
public class UserFacade extends AbstractFacade<User> {

    @PersistenceContext(unitName = XSC_PU_NAME)
    private EntityManager em;
    private static final Logger LOGGER = Logger.getLogger(UserFacade.class.getName());

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserFacade() {
        super(User.class);
    }

    public User findByUsername(String username) {
        Query query = getEntityManager().createNamedQuery("User.findByUsername");
        query.setParameter("username", username);
        try {
            return (User) query.getSingleResult();
        } catch (NoResultException e) {
            LOGGER.log(Level.WARNING, "User " + username + " not found.", e);
            throw e;
        }
    }
}
